package com.ohuang.test_compose.composeView


import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.Layout

/**
 * 显示隐藏布局
 *  能够显示隐藏布局，但是不占用空间
 *  不会让子布局退出重组，能够很好的保存状态
 */
@Composable
fun VisibilityLayout(modifier: Modifier= Modifier, isVisibility: Boolean=true,  content: @Composable () -> Unit){
    Layout(
        content = content,
        modifier = modifier
    ) { measurables, constraints ->
        if (isVisibility) {
            // 正常布局
            val placeables = measurables.map { it.measure(constraints) }
            layout(constraints.maxWidth, constraints.maxHeight) {
                placeables.forEach { it.placeRelative(0, 0) }
            }
        } else {
            // 隐藏但仍占用空间
            layout(0, 0) {}
        }
    }
}